package cn.zhuzicc.reflect;

/**
 * @author zx
 * @version 1.0
 * @className: Test03
 * @description: Class类创建方式有哪些
 * @date: 2021/4/25 18:08
 */
public class Test03 {
    public static void main(String[] args) throws ClassNotFoundException {
        Person person = new Student();
        System.out.println("身份：" + person.name);

        // 方式一 通过对象获得
        Class c1 = person.getClass();
        System.out.println(c1.hashCode());

        // 方式二 Class.forName获取
        Class c2 = Class.forName("cn.zhuzicc.reflect.Student");
        System.out.println(c2.hashCode());

        // 方式三 类名.class
        Class c3 = Student.class;
        System.out.println(c3.hashCode());

        // 方式四 基本类型包装类.TYPE
        Class c4 = Integer.TYPE;
        System.out.println(c4);

        // 方式五 获取父类类型
        Class c5 = c1.getSuperclass();
        System.out.println(c5);
    }
}

class Person {

    public String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                '}';
    }
}

class Student extends Person {

    public Student() {
        this.name = "学生";
    }
}

class Teacher extends Person {

    public Teacher() {
        this.name = "老师";
    }
}